package leetcode.part1;

import org.junit.Test;

/**
 * 分发糖果
 */
public class S135 {
    public int candy(int[] ratings) {
        int n = ratings.length;
        int[] nums=new int[n];
        for (int i = 0; i < n; i++) {
            if (i > 0 && ratings[i] > ratings[i - 1]) {
                nums[i] = nums[i - 1] + 1;
            } else {
                nums[i] = 1;
            }
        }
        int right = 0;
        int count = 0;
        for (int i = n-1; i >=0 ; i--) {
            if (i<n-1&&ratings[i]>ratings[i+1]){
                right++;
            }else {
                right=1;
            }
            count+=Math.max(right,nums[i]);
        }


        return count;
    }

    @Test
    public void test() {
        int[] ratings={1,2,2};
        System.out.println(candy(ratings));
    }
}
